package com.solocredit.finance.utils.helper

import com.solocredit.finance.app.MyApp
import com.solocredit.finance.http.ApiService
import com.solocredit.finance.http.ApiService.addCommonParams
import com.solocredit.finance.http.ApiService.addRandomParams
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.http.IHttpCallback
import com.solocredit.finance.utils.event.MullaEventData
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/16 15:49
 * @Annotation：用户积分
 */
object MullaUserPointsHelper {

    /**
     * 保存、使用 用户积分
     */
    fun saveMullsUserPointsAction(useType: String,useInfo: String,points: String,callback: IHttpCallback? = null){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_PATH.MULLA_API_SAVE_OR_USE_PATH)
        val params = JSONObject().addCommonParams().addRandomParams().apply {
            put("mobile", SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_MOBILE))
            put("scoreUseType",useType)//分数使用类型
            put("scoreUseInfo", useInfo)//分数使用说明 , 积分列表展示
            put("score", points)//增加传正数，减少传负数
        }
        HttpHelper.postMullaAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                callback?.onRequestSuccess(data)
                when(data.optInt("code")) {
                    0 -> {//成功
                        //积分存、去成功  需要刷新签到信息
                        MyApp.app.publisher.publish(MullaEventData(MullaEventData.TYPE.MULLA_POINTS_REFRESH))
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                callback?.onRequestError(error)
            }

            override fun onRequestFinish() {
                callback?.onRequestFinish()
            }
        })
    }
}